//
//  BKTopScrollViewHeaderView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/15.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKTopScrollViewHeaderView.h"
#import "masonry.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]

@interface BKTopScrollViewHeaderView ()

@property (nonatomic,strong)UIScrollView   *scrView;
@property (nonatomic,strong)NSMutableArray *btnArr; //titleBtn数组
@property (nonatomic,strong)NSArray  *nameArr;
@property (nonatomic,strong)UIView   *bottomLineView; //底部指示view
@property (nonatomic,strong)UIView   *bottomLineEnd; //底部分割线
@property (nonatomic,assign)CGFloat   scrContentOffsetWidth;
@property (nonatomic,strong)UIButton *isSeletedBtn;

@end
@implementation BKTopScrollViewHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        self.backgroundColor = [UIColor whiteColor];
       
    }
    return self;
}

-(void)setNameArray:(NSArray *)nameArray{
    _nameArray = nameArray;
    [self setupViewWithCategoryNames:_nameArray];
}

-(void)setCurPage:(NSInteger)curPage{
    if (_curPage == curPage) {
        return;
    }
    
    _curPage = curPage;
    if (curPage > self.btnArr.count || curPage == self.btnArr.count) {
        return;
    }
    
    UIButton *btn = self.btnArr[_curPage];
    [self titleBtnTouch:btn];
    
}

-(void)setupViewWithCategoryNames:(NSArray *)names
{
    self.nameArr = names;
    UIScrollView *scView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 34)];
    self.scrView = scView;
    CGFloat width = [self widthOfScrowContentSizeWithCategoryNames:names];
    self.scrContentOffsetWidth = width;
    scView.contentSize = CGSizeMake(width, 30);
    scView.showsVerticalScrollIndicator = NO;
    scView.showsHorizontalScrollIndicator = NO;
    scView.delegate = self;
    scView.scrollEnabled = YES;
    [self addSubview: scView];

    _btnArr = [NSMutableArray array];
    for (NSInteger i = 0; i < names.count; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i + 3000;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:names[i] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
        [btn setTitleColor:RGB(3, 181, 152) forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(titleBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0) {
            btn.selected = YES;
            self.isSeletedBtn = btn;
        }
        [self.scrView addSubview:btn];
        [_btnArr addObject:btn];
    }
    
    self.bottomLineView = [[UIView alloc]init];
    self.bottomLineView.backgroundColor = RGB(3, 181, 152);
    [_scrView addSubview:self.bottomLineView];
    
    self.bottomLineEnd = [[UIView alloc]init];
    self.bottomLineEnd.backgroundColor = RGB(238, 238, 238);
    [self addSubview:self.bottomLineEnd];
    
    [self layout];
    
}

//button点击方法
-(void)titleBtnTouch:(UIButton *)button
{
    if (button == self.isSeletedBtn) {
        
        return;
    }
    
    button.selected = YES;
    self.isSeletedBtn.selected = NO;
    self.isSeletedBtn = button;
    
    //设置指示条位置
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(button.mas_centerX).offset(0);
            make.top.mas_equalTo(button.mas_bottom).offset(0);
            make.width.mas_equalTo(button.mas_width);
            make.height.mas_equalTo(@(2));
        }];
        
    }];
    
    
    if ([self.delegate respondsToSelector:@selector(BKTopScrollViewHeaderView:index:categroName:)]) {
        
        [self.delegate BKTopScrollViewHeaderView:self index:button.tag - 3000 categroName:self.nameArr[button.tag - 3000]];
    }
    
    [self clickBtnChangeBtnLocationWithBtn:button];
}
-(void)layout
{
    CGFloat ViewLeft = 0;
    for (int i = 0; i < self.btnArr.count; i++) {
        
        UIButton * btn = self.btnArr[i];
        
        CGFloat width = [self sizeWithContentString:self.nameArr[i] textFont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenWidth, 30)].width;
        
        if (i == 0) {
            ViewLeft = 10;
        }
        
        btn.frame = CGRectMake(ViewLeft, 0, width, 32);
        ViewLeft = btn.frame.origin.x + btn.frame.size.width + 30;
        
    }
    
    CGFloat width = [self sizeWithContentString:self.nameArr[0] textFont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenWidth, 30)].width;
    
    [_bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(32);
        make.left.mas_equalTo(self.scrView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 2));
        
    }];
    
    [_bottomLineEnd mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@(0.5));
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.bottom.mas_equalTo(_bottomLineView.mas_bottom).offset(0);
        
    }];
    

}

// 让选中的按钮居中显示
-(void)clickBtnChangeBtnLocationWithBtn:(UIButton *)button
{
    // 设置标题滚动区域的偏移量, 如果点击的label超过屏幕一半的位置, 要把它移动到中间, 即为偏移量
    CGFloat offsetX = button.center.x - ScreenWidth * 0.5;
    
    if (offsetX < 0) {
        offsetX = 0;
    }
    
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = self.scrView.contentSize.width - ScreenWidth + 10;
    
    if (maxOffsetX < 0) {
        maxOffsetX = 0;
    }
    
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    
    // 滚动区域
    [self.scrView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}


-(CGFloat)widthOfScrowContentSizeWithCategoryNames:(NSArray*)names
{
    CGFloat totalWidth = 0;
    CGFloat lineSpace = 30;
    
    for (int i = 0; i < names.count; i++ ) {
        
        CGFloat  width = [self sizeWithContentString:names[i] textFont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenWidth, 30)].width;
        totalWidth += (lineSpace + width);
    }
    
    if (totalWidth + 10 < ScreenWidth ) {
        
        return ScreenWidth;
    }
    
    return totalWidth+10;
}
-(CGSize)sizeWithContentString:(NSString *)content textFont:(UIFont *)font size:(CGSize)contentSize
{
    NSDictionary * fontDict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGRect rect = [content boundingRectWithSize:contentSize options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
    
    return rect.size;
}
//更改iOS7 时contentoffsize无法更改的问题
-(void)changeScroviewContentOffset
{
    _scrView.contentSize = CGSizeMake(self.scrContentOffsetWidth, 30);
}

@end
